package mongodb

import (
	"context"
	"time"

	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

//Collection ...
var collection *mongo.Database
var err error

//ConnectToDB ...
func connectToDB(uri, name string, timeout time.Duration, num uint64) (*mongo.Database, error) {
	ctx, cancel := context.WithTimeout(context.Background(), timeout)
	defer cancel()
	o := options.Client().ApplyURI(uri)
	o.SetMaxPoolSize(num)
	client, err := mongo.Connect(ctx, o)
	if err != nil {
		return nil, err
	}

	return client.Database(name), nil
}
func init() {
	collection, err = connectToDB("mongodb://localhost:27017", "user", 5*time.Second, 10)
	if err != nil {
		return
	}
}

//Get collection
func Get() *mongo.Database {
	return collection
}
